//
//  MingXiTab.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/24.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "MingXiTab.h"
#import "Mingxicell.h"
#import "MingXiTabHeader.h"
#import "MJRefresh.h"
//明细接口
#define MINGXI_URL @"/api/ApiUser/getAccountDetail"
@interface MingXiTab ()<UITableViewDataSource,UITableViewDelegate>
{
    MingXiType _type;
    NSMutableArray *_data;
    MingXiTabHeader *_header;
    NSInteger _currentPage;
}
@end
@implementation MingXiTab
- (instancetype)initWithFrame:(CGRect)frame AndType:(MingXiType)type
{
    if (self = [super initWithFrame:frame]) {
        _data = [NSMutableArray array];
        _currentPage = 1;
        _type = type;
        self.dataSource = self;
        self.delegate = self;
        [self registerNib:[UINib nibWithNibName:@"Mingxicell" bundle:nil] forCellReuseIdentifier:@"cell"];
        self.rowHeight = 28.0;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        _header = [[[NSBundle mainBundle]loadNibNamed:@"MingXiTabHeader" owner:self options:nil] lastObject];
        self.tableHeaderView = _header;
        self.tableFooterView = [[UIView alloc]init];
        [self requestForMingxi];
        // 2.集成刷新控件
        [self setupRefresh];
    }
    return self;
}
/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    //    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    // dateKey用于存储刷新时间，可以保证不同界面拥有不同的刷新时间
    [self addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self addFooterWithTarget:self action:@selector(footerRereshing)];
}
#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    _currentPage = 1;
    [_data removeAllObjects];
    [self requestForMingxi];
}

- (void)footerRereshing
{
    _currentPage++;
    [self requestForMingxi];
}

- (void)requestForMingxi
{
    NSString *filter = nil;
    switch (_type) {
        case DUOBI:
            filter = @"10";
            break;
        case JIFEN:
            filter = @"20";
            break;
        default:
            break;
    }
    [WYPublic addHudToWindow];
    NSDictionary *params = @{@"filter":filter,@"page":[NSNumber numberWithInteger:_currentPage],@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
    [WYRequest  startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,MINGXI_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        
        NSNumber *ret = dic[@"ret"];
        NSDictionary *info = dic[@"data"];
        if (ret.intValue == 0) {
            switch (_type) {
                case DUOBI:
                {
                    NSNumber *num = info[@"money"];
                    [_header reloadHeaderWith:[WYPublic getRedMiddleStrWithLeft:@"总夺币金额:" Middle:[NSString stringWithFormat:@"%ld",(unsigned long)[num integerValue]] RightStr:@"" Font:14]];
                }
                    break;
                case JIFEN:
                {
                     NSNumber *num = info[@"score"];
                    [_header reloadHeaderWith:[WYPublic getRedMiddleStrWithLeft:@"总积分金额:" Middle:[NSString stringWithFormat:@"%ld",(unsigned long)[num integerValue]] RightStr:@"" Font:14]];
                }
                    
                    break;
                default:
                    break;
            }
            NSArray *list = info[@"list"];
            for (NSDictionary *mx in list) {
                MingxiModel *model = [[MingxiModel alloc]init];
                model.time = mx[@"time"];
                model.came = mx[@"full_pay_type_desc"];
                model.money = mx[@"amount_show"];
                [_data addObject:model];
            }
            [self reloadData];
        }else{
            if(ret.intValue>2000 && ret.intValue < 3000 && _type == DUOBI){
                [[WYMainTabBarVC shareMainTaBarVC].navigationController popViewControllerAnimated:NO];
                //用户过期，强制重新登录 先删除用户信息
                [UserInfo deleteUserInfo];
                //登录
                [WYPublic ifUserHaveLogin];
                [WYPublic showMessageWithTitle:@"提示：" Msg:@"登录信息已过期"];
            }
            
        }
        
        [WYPublic removeHudToWindow];
        [self headerEndRefreshing];
        [self footerEndRefreshing];
    } failedBlock:^{
        [WYPublic removeHudToWindow];
        [self headerEndRefreshing];
        [self footerEndRefreshing];
    } needLog:NO];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Mingxicell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (_data.count > 0) {
        MingxiModel *model = _data[indexPath.row];
        [cell fillCellWithModel:model];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
